<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="资产类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="asset_type" placeholder="请选择资产类型" />
            </a-form-model-item>
          </a-col>
          <span v-if="model.type==='1'">
            <a-col :span="12">
              <a-form-model-item label="资产名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
                <a-input v-model="model.name" placeholder="请输入资产名称"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="使用功能" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useDesc">
                <a-input v-model="model.useDesc" placeholder="请输入使用功能"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="地址/位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
                <a-input v-model="model.position" placeholder="请输入地址/位置"  ></a-input>
                <!-- <j-dict-select-tag type="list" v-model="model.position" dictCode="asset_ledger,name,id" placeholder="请选择地址/位置" /> -->
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="资产状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
                <j-dict-select-tag type="list" v-model="model.status" dictCode="asset_status_space" placeholder="请选择资产状态" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="空间类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spaceType">
                <j-dict-select-tag type="list" v-model="model.spaceType" dictCode="asset_build_type" placeholder="请选择空间类型" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chargePerson">
                <a-input v-model="model.chargePerson" placeholder="请输入负责人"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="建筑面积（㎡）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="floorArea">
                <a-input-number style="width:100%" v-model="model.floorArea" placeholder="请输入建筑面积（㎡）"  ></a-input-number>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="资产价值（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
                <a-input-number style="width:100%" v-model="model.price" placeholder="请输入资产价值"  ></a-input-number>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="files">
                <j-upload v-model="model.files"   ></j-upload>
              </a-form-model-item>
            </a-col>
          </span>
          <span v-if="model.type==='2'">
            <a-col :span="12">
              <a-form-model-item label="资产名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
                <a-input v-model="model.name" placeholder="请输入资产名称"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="资产状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
                <j-dict-select-tag type="list" v-model="model.status" dictCode="asset_status_equip" placeholder="请选择资产状态" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="设备类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equiCategory">
                <j-dict-select-tag type="list" v-model="model.equiCategory" dictCode="asset_equip_type" placeholder="请选择设备类别" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="出厂日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionDate">
                <j-date placeholder="请选择出厂日期" v-model="model.productionDate"  style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="model">
                <a-input v-model="model.model" placeholder="请输入规格型号"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chargePerson">
                <a-input v-model="model.chargePerson" placeholder="请输入负责人"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="制造商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="producer">
                <a-input v-model="model.producer" placeholder="请输入制造商"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="安装地点（空间）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
                <j-dict-select-tag type="list" v-model="model.position" dictCode="asset_ledger,name,id,del_flag='0' and type='1'" placeholder="请选择地址/位置" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="资产价值（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
                <a-input-number style="width:100%" v-model="model.price" placeholder="请输入资产价值"  ></a-input-number>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="files">
                <j-upload v-model="model.files"   ></j-upload>
              </a-form-model-item>
            </a-col>
          </span>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AssetLedgerForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          type: '1',
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: [{required: true, message: '请输入资产名称!'}],
          status: [{required: true, message: '请选择资产状态!'}],
        },
          
        url: {
          add: "/asset/assetLedger/add",
          edit: "/asset/assetLedger/edit",
          queryById: "/asset/assetLedger/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>